package org.jeecg.modules.wms.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.wms.entity.WmsSupplierMaterial;
import org.jeecg.modules.wms.service.IWmsSupplierMaterialService;
import org.jeecg.modules.wms.vo.WmsSupplierMaterialVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 供应商物料关联管理
 * @Author: jeecg-boot
 * @Date: 2023-07-18
 * @Version: V1.0
 */
@Api(tags="供应商物料关联管理")
@RestController
@RequestMapping("/wms/supplier/material")
@Slf4j
public class WmsSupplierMaterialController extends JeecgController<WmsSupplierMaterial, IWmsSupplierMaterialService> {
    @Autowired
    private IWmsSupplierMaterialService wmsSupplierMaterialService;
    
    /**
     * 分页列表查询
     */
    @ApiOperation(value="供应商物料关联列表", notes="获取供应商物料关联列表")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(WmsSupplierMaterial wmsSupplierMaterial,
                                @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                HttpServletRequest req) {
        QueryWrapper<WmsSupplierMaterial> queryWrapper = QueryGenerator.initQueryWrapper(wmsSupplierMaterial, req.getParameterMap());
        Page<WmsSupplierMaterial> page = new Page<WmsSupplierMaterial>(pageNo, pageSize);
        IPage<WmsSupplierMaterial> pageList = wmsSupplierMaterialService.page(page, queryWrapper);
        return Result.OK(pageList);
    }
    
    /**
     * 添加
     */
    @ApiOperation(value="添加供应商物料关联", notes="添加供应商物料关联")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody WmsSupplierMaterial wmsSupplierMaterial) {
        wmsSupplierMaterialService.save(wmsSupplierMaterial);
        return Result.OK("添加成功！");
    }
    
    /**
     * 编辑
     */
    @ApiOperation(value="编辑供应商物料关联", notes="编辑供应商物料关联")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody WmsSupplierMaterial wmsSupplierMaterial) {
        wmsSupplierMaterialService.updateById(wmsSupplierMaterial);
        return Result.OK("编辑成功!");
    }
    
    /**
     * 通过id删除
     */
    @ApiOperation(value="删除供应商物料关联", notes="删除供应商物料关联")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id") String id) {
        wmsSupplierMaterialService.removeById(id);
        return Result.OK("删除成功!");
    }
    
    /**
     * 批量删除
     */
    @ApiOperation(value="批量删除供应商物料关联", notes="批量删除供应商物料关联")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids") String ids) {
        this.wmsSupplierMaterialService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }
    
    /**
     * 通过id查询
     */
    @ApiOperation(value="供应商物料关联详情", notes="供应商物料关联详情")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id") String id) {
        WmsSupplierMaterial wmsSupplierMaterial = wmsSupplierMaterialService.getById(id);
        if(wmsSupplierMaterial==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(wmsSupplierMaterial);
    }
    
    /**
     * 根据供应商ID获取物料关联列表
     */
    @ApiOperation(value="根据供应商ID获取物料关联列表", notes="根据供应商ID获取物料关联列表")
    @GetMapping(value = "/listBySupplier")
    public Result<?> listBySupplier(@RequestParam(name="supplierId") String supplierId) {
        List<WmsSupplierMaterial> list = wmsSupplierMaterialService.listBySupplier(supplierId);
        return Result.OK(list);
    }
    
    /**
     * 根据物料ID获取关联的供应商列表
     */
    @ApiOperation(value="获取物料关联的供应商列表", notes="根据物料ID获取关联的供应商列表")
    @GetMapping(value = "/suppliers")
    public Result<?> getMaterialSuppliers(@RequestParam(name="materialCode", required=false) String materialCode) {
        List<WmsSupplierMaterialVO> list;
        if (materialCode != null && !materialCode.isEmpty()) {
            // 当物料编码不为空时，查询指定物料的供应商
            list = wmsSupplierMaterialService.getSuppliersByMaterialCode(materialCode);
        } else {
            // 当物料编码为空时，查询所有供应商
            list = wmsSupplierMaterialService.getAllSuppliers();
        }
        return Result.OK(list);
    }
    
    /**
     * 导出excel
     */
    @AutoLog(value = "供应商物料关联-导出")
    @ApiOperation(value="供应商物料关联-导出", notes="供应商物料关联-导出")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, WmsSupplierMaterial wmsSupplierMaterial) {
        return super.exportXls(request, wmsSupplierMaterial, WmsSupplierMaterial.class, "供应商物料关联");
    }
    
    /**
     * 通过excel导入数据
     */
    @AutoLog(value = "供应商物料关联-导入")
    @ApiOperation(value="供应商物料关联-导入", notes="供应商物料关联-导入")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, WmsSupplierMaterial.class);
    }
} 